package com.person.mengshuai.mytestdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.mengshuai.mytestdemo.entity.BRole;
import com.person.mengshuai.mytestdemo.entity.BUserRole;
import com.person.mengshuai.mytestdemo.mapper.BRoleMapper;
import com.person.mengshuai.mytestdemo.mapper.BUserRoleMapper;
import com.person.mengshuai.mytestdemo.service.BRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统管理-角色表  服务实现类
 * </p>
 *
 * @author ms
 * @since 2024-08-02
 */
@Service
public class BRoleImpl extends ServiceImpl<BRoleMapper, BRole> implements BRoleService {
    @Autowired
    private BUserRoleMapper userRoleMapper;

    @Override
    public List<String> getRoleNamesByUserId(String userId) {
        List<BRole> bRoles = getBRoleListByUserId(userId);
        return CollectionUtils.isEmpty(bRoles) ? null :
                bRoles.stream().map(BRole::getName).collect(Collectors.toList());
    }

    @Override
    public List<BRole> getBRoleListByUserId(String userId) {
        LambdaQueryWrapper<BUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BUserRole::getUserId, userId);
        List<BUserRole> bUserRoles = userRoleMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(bUserRoles)) {
            return null;
        }
        List<Integer> roleIds = bUserRoles.stream().map(BUserRole::getRoleId).collect(Collectors.toList());
        return baseMapper.selectBatchIds(roleIds);
    }
}
